<?php
	class categoryControl extends commonControl{
		/**
		 * 显示所有栏目
		 */
		public function index(){
			$db = V("category");
			$db->view = array(
						'model'=>array(
									'type'=>'inner',
									'on'=>'category.mid = model.mid',
								),
					);
			$category = $db->field("catid,cname,category.sort,pid,category.mid,mname,model.title|modeltitle")->all();
			//var_dump($category);
			$category = arr::channel($category,"catid","pid",0,0,2,"&nbsp;&nbsp;&nbsp;&nbsp;");
			$this->assign("category",$category);
			
			$this->display();
		}
		/**
		 * 增加栏目show
		 */
		public function add_show(){
			$pid = isset($_GET['catid'])?$_GET['catid']:0; //栏目的父ID
			$model = M('model');
			$models = $model->where(" state = 1")->all();
			$this->assign('models', $models); //发送模型
			
			$role = M('role');
			$admin = $role->where("isadmin=1")->order("rid asc")->all();
        	$user = $role->where("isadmin=0")->order("rid asc")->all();
			$this->assign('admin', $admin);
			$this->assign('user', $user);
			$this->display();
		}
		/**
		 * 执行增加栏目
		 */
		public function add_exe(){
			$db = M("category");
			$catid = null; //插入成功时返回的catid
			
			$db->validate = array(
						array('cname','nonull','栏目名称必填',2),
						array('catdir','regexp:/^[a-z][a-z0-9_]+/i','栏目目录名必须为英文',2),
					);
			if(!$db->validate){
				$this->error($db->error);
			}else{
		        if(!!$catid = $db->add()){
		        	var_dump($catid);
		        	$this->update_category_access($catid);
		            $this->success("添加栏目成功",'index');
		        }else{
		            $this->error($db->error);
		        }
			}
		}
		/**
		 * 更新用户的栏目权限
		 * @param number|string $catid
		 */
		public function update_category_access($catid){
			$db = M("category_access");
			$error = 0;
			if (isset($_POST['category_access'])) {
				$count = count($_POST['category_access']);
				$db->where("catid in ($catid)")->del();
				foreach ($_POST['category_access'] as $v) {
					$arr = explode(",", $v);
					$category_access_data['rid'] = $arr[0];
					$category_access_data['rtitle'] = $arr[1];
					$category_access_data['catid'] = $catid;
					if($db->add($category_access_data)){
						$error++;
					}
				}
				if($error != 0){
					return false;
				}else{
					return true;
				}
			}else{
				return false;
			}
		}
		/**
		 * 栏目删除
		 */
		public function del(){
			$cat = M('category');
			if($cat->where("pid='".$_GET['catid']."'")->find()){
				$this->error('该栏目下还有子栏目，不能删除');
			}else{
				if($cat->table('news')->where("catid = '".$_GET['catid']."'")->find()){
					$this->error('该栏目下还有文章，不能删除');
				}else{
					if($cat->table('category')->where("catid='".$_GET['catid']."'")->del()){
						$this->success('恭喜删除成功');
					}else{
						$this->error('删除失败');
					}
				}
			}
		}
		/**
		 * 栏目修改
		 */
		public function cat_modify(){
			if(!isset($_GET['catid'])){
				$this->error('不好意思，操作错误！');
				return;
			}
			$catid = $_GET['catid']; //要修改的栏目的id
			$mid = $_GET['mid']; //要修改的栏目的模型id
			$cat = M('category');
			$cat_list = $cat->where(" catid = '".$catid."'")->find(); //获得要修改的栏目的信息
			$cat_pid = $cat_list['pid']; //要修改的栏目的父栏目
			$this->assign('cat_list', $cat_list);
			//获得所有栏目列表
			$category = $cat->field("catid,cname,sort,pid")->where(" mid = $mid")->all();
			$category = delStrValue($category,'catid',$catid);//去除修改栏目的一个选项
			if($cat_pid == 0 ){ //当栏目为顶级栏目的时候，不能修改栏目
				$category = array();
			}
			$category = arr::channel($category,"catid","pid",0,2,"&nbsp;&nbsp;&nbsp;"); //数组转换,重新组合html空格
			$this->assign("category",$category);
			//栏目权限角色
			$role = M('role');
			$admin = $role->where("isadmin=1")->order("rid asc")->all();
        	$user = $role->where("isadmin=0")->order("rid asc")->all();
			//栏目权限显示
			$cat_acc = R('category');
			$cat_acc->join = array(
					'category_access'=>array(
							"type" => "has_many",
							"foreign_key" => "catid",
							"other" => 1,
						),
				);
			$cat_acc_rows = $cat_acc->where(" catid = '".$catid."'")->find();
			$admin = $this->getUserAccess($admin, $cat_acc_rows['category_access']);
			$user = $this->getUserAccess($user, $cat_acc_rows['category_access']);
			
			$this->assign('admin', $admin); //后台用户
			$this->assign('user', $user); //前台用户
			$this->display();
		}
		/**
		 * 查询用户权限
		 * @param 角色 Array1维 $role
		 * @param 权限 Array1维 $access
		 */
		public function getUserAccess($role,$access){
			foreach($role as $k=>$v){
				$role[$k]['access'] = array();
				if(empty($access)){
					continue;
				}
				foreach($access as $key=>$value){
					if($v['rid'] == $value['rid']){
						$role[$k]['access'][] = $value['rtitle'];
					}
				}
			}
			return $role;
		}
		/**
		 * 执行栏目修改
		 */
		public function modify_exe(){
			if(!isset($_POST['catid'])){
				$this->error('参数错误');
			}
			$catid = $_POST['catid'];
			$cat = M('category');
			$cat->validate = array(
					array('cname','nonull','栏目名称必填',2),
					array('catdir','regexp:/^[a-z][a-z0-9_]+/i','栏目目录名必须为英文',2),
			);
			if(!$cat->validate()){
				$this->error();
			}else{
				$cat->where(" catid = '".$catid."'")->update();
				if($this->update_category_access($catid)){
					$this->success('恭喜，修改成功',__WEB__.'/admin/category/index');
				}else{
					$this->error('可惜，修改栏目失败了！');
				}
			}
		}
		/**
		 * 选择栏目模版
		 */
		public function selecttemplate(){
			C("debug", 0);
			$default_tpl = include PATH_ROOT.'/bccms/index/config/config.php'; //用来保存网站前台的模版
			$default_dir = $default_tpl['TPL_DIR'];
			$default_tpl = $default_tpl['TPL_STYLE'];
			$path = $default_dir . '/' . $default_tpl;
			$files = dir::tree($path, 'html');
			$this->assign("tplpath", $path);
			$this->assign("files", $files);
			$this->display();
		}
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
?>